/**
 * Created by home on 2019/9/28.
 * @author cyy
 */
layui.define(['element','laytpl','table','upload','form'],function (exports) {
    var element = layui.element
        , laytpl = layui.laytpl
        , table = layui.table
        , upload = layui.upload
        ,form = layui.form;


    /*csrf安全*/
    $(function () {
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        $(document).ajaxSend(function(e, xhr, options) {
            xhr.setRequestHeader(header, token);
        });
    });


    table.render({
        elem: '#passRecommend'
        ,toolbar:'#toolbarEdit'
        ,url:'/admin/recommend/articleRecommendList'
        ,cellMinWidth: 60 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,page: true
        , request: {
            pageName: 'pageNumber' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        ,cols: [[
            {field:'recommendedTime',  title: '推荐时间', sort: true}
            ,{field:'article', title: '标题',templet:'<div>{{d.article.title}}</div>'}
            ,{field:'article', title: '作者',templet:'<div>{{d.article.witter}}</div>'}
            ,{field:'recommendedReason', title: '推荐理由'}
            ,{field:'indexShowImageUrl', title: '展示图片地址'}
            ,{fixed: 'right',title:'操作' ,align:'center', toolbar: '#recommendBar'}
        ]]

    });


    /*监听行工具事件*/
    table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if(layEvent == 'cancelRecommend'){
            /*取消推荐*/
            $.post('/admin/recommend/cancelRecommend/' + data.id, {}, function (res, stats) {
                if (stats && stats === 'success') {
                    layer.msg("取消成功！");
                    table.reload('passRecommend', {
                        url: '/admin/recommend/articleRecommendList'
                        ,where: {} //设定异步数据接口的额外参数
                        //,height: 300
                    });
                } else {
                    layer.msg("取消失败！")
                }
            })
        }
        else if(layEvent === 'LAYTABLE_TIPS'){
            layer.alert('Hi，头部工具栏扩展的右侧图标。');
        }
    });



    //头工具栏事件
    table.on('toolbar(test)', function(obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'editRecommend':
                layer.open({
                    type: 2
                    , offset: 'auto' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
                    , id: 'commend'
                    , title: '设置推荐'
                    , content: '/admin/recommend/recommendInformation/'
                    , area: ['800px', '500px']
                    , btn: ['关闭']
                    , shade: 0 //不显示遮罩
                    , yes: function (index, layero) {
                        layer.close(index)
                    },success: function () {

                    },end:function () {
                        table.reload('passRecommend', {
                            url:'/admin/recommend/articleRecommendList'
                            ,where: {} //设定异步数据接口的额外参数
                            //,height: 300
                        });
                    }
                });
        }
    });




    exports('recommend', {});
});
